package com.tenglong.teacher.ui.setting.setting;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.Utils;
import com.cuotibao.zhuoxiao.basics.utils.recyview.DividerSpaceItemDecoration;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.InfomMessageInfo;
import com.tenglong.teacher.data.resultInfo.MessageInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.adapter.MessageAdapter;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class InformActivity extends BaseActivity implements OnRefreshListener, MessageAdapter.MessageListener {

    private RecyclerView mRecyclerView;
    private List<MessageInfo> infos;
    private MessageAdapter mAdapter;
    private View noData;
    private RefreshLayout mSmartRefreshLayout;
    private boolean isFirstDownLoad = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inform);
        initView();
        initRecy();
        setAdapte();
        setListeners();
        request();
    }

    private void setListeners() {
        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setOnRefreshListener(this);
        mAdapter.setMessageListener(this);
    }

    private void setAdapte() {
        infos = new ArrayList<>();
        mAdapter = new MessageAdapter(infos);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void request() {
        if (isFirstDownLoad) {
            isFirstDownLoad = false;
            showProgressDialog("数据加载中...");
        }
        CreateRequestEntity.getWebService().getInfomMessageInfo().enqueue(new Callback<BaseResult<List<InfomMessageInfo>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<InfomMessageInfo>>> call, Response<BaseResult<List<InfomMessageInfo>>> response) {
                if (ResultVerifier.isSucceed(response)) {
                    infos.clear();
                    infos.addAll(response.body().data);
                    mAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
                mSmartRefreshLayout.finishRefresh(true);
                noData.setVisibility(infos.size() == 0 ? View.VISIBLE : View.GONE);
                dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseResult<List<InfomMessageInfo>>> call, Throwable t) {
                ResultVerifier.systerErrers(t);
                noData.setVisibility(infos.size() == 0 ? View.VISIBLE : View.GONE);
                dismissProgressDialog();
                mSmartRefreshLayout.finishRefresh(false);
            }
        });
    }

    private void initRecy() {
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerSpaceItemDecoration(Utils.dip2px(this, 15)));
    }

    private void initView() {
        setTitle("通知消息");
        mRecyclerView = findViewById(R.id.mRecyclerView);
        noData = findViewById(R.id.noData);
        mSmartRefreshLayout = findViewById(R.id.mSmartRefreshLayout);
        ClassicsHeader headerView = new ClassicsHeader(this);
        ClassicsFooter footView = new ClassicsFooter(this);
        headerView.setBackgroundColor(Color.parseColor("#f8f8f8"));
        footView.setBackgroundColor(Color.parseColor("#f8f8f8"));
        mSmartRefreshLayout.setRefreshHeader(headerView);
        mSmartRefreshLayout.setRefreshFooter(footView);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        request();
    }

    @Override
    public void onInformListener(int position) {
        //通知点击事件
    }

    @Override
    public void onVerifyMessageEvent(int position, boolean isAgree) {

    }
}
